package com.tuanzi.loan.web.bean.collaborate;

import com.tuanzi.loan.business.entity.CurrentUser;
import com.tuanzi.loan.business.entity.LoginUser;
import com.tuanzi.loan.business.entity.family.LoanLargeAmountNewPartnerInfo;
import com.tuanzi.loan.business.service.family.LoanLargeAmountNewPartnerInfoService;
import com.tuanzi.loan.vo.request.family.largeamount.LoanStatLargePageDetailRequest;
import com.tuanzi.loan.vo.response.family.largeamount.LoanStatLargePageDetailResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.collaborate.LargePartnerStatListBeanLazyDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.LocalDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;

/**
 * User: liangZongXi
 * Create time: 14:45 2019/8/21
 * Description:
 */
@Data
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class LargePartnerStatListBean extends JSFListingBean<LoanStatLargePageDetailRequest, LoanStatLargePageDetailResponse> {
    private static final long serialVersionUID = -7844594648044817631L;
    @Autowired
    private LoanLargeAmountNewPartnerInfoService loanLargeAmountNewPartnerInfoService;
    private String[] totalChannel;


    @PostConstruct
    private void init(){
        initTotalChannel();
        initLazyDataModel();
    }

    private void initLazyDataModel() {
        $ = new LoanStatLargePageDetailRequest();
        LocalDate now = LocalDate.now();
        LocalDate before = now.plusDays(-7);
        $.setStartDate(before.toDate());
        $.setEndDate(now.toDate());
        this.lazyDataModel = new LargePartnerStatListBeanLazyDataModel($);
    }

    private void initTotalChannel() {
        LoginUser loginUser = CurrentUser.get();
        Assert.notNull(loginUser, "无法获取用户账号");
        String account = loginUser.getAccount();
        LoanLargeAmountNewPartnerInfo allByPartnerAccount = loanLargeAmountNewPartnerInfoService.findAllByPartnerAccount(account);
        if(null != allByPartnerAccount){
            String channelNdInfo = allByPartnerAccount.getChannelNdInfo();
            if(StringUtils.isNotBlank(channelNdInfo)){
                this.totalChannel = channelNdInfo.split(",");
            }
        }
    }

    public void onReset(){
        initLazyDataModel();
    }
}
